#!/bin/sh
#
# @license   http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author    Ian Moore <imooreyahoo@gmail.com>
# @copyright Copyright (c) 2011 Ian Moore
#
# This file is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this file. If not, see <http://www.gnu.org/licenses/>.

set -e

. /etc/default/openmediavault
. /usr/share/openmediavault/scripts/helper-functions

SITECONF="zzz-omv-website-vhost"
SITECONFFILE="/etc/apache2/sites-available/${SITECONF}"

# Enable/disable site
if [ "$(omv_config_get "//services/website/enable")" != "1" ]; then
	a2dissite "$SITECONF"
	exit 0
fi

USESSL="$(omv_config_get "//services/website/enablessl")"

# Port or name based vhost
if [ "$(omv_config_get "//services/website/vhosttype")" = "port" ]; then

	port=$(omv_config_get "//services/website/port")

	# Update apache's ports if it does not already exist
	if [ $(egrep -ic "^Listen\s+${port}$" /etc/apache2/ports.conf) -lt 1 ]; then
		echo "Listen ${port}" >>/etc/apache2/ports.conf
	fi

	# Add to vhost config
	echo "<VirtualHost *:${port}>" >${SITECONFFILE}

else

	# Use OMV port
	if [ "$(omv_config_get "//webadmin/forcesslonly")" = "1" ]; then
		port=$(omv_config_get "//webadmin/sslport")
	else
		port=$(omv_config_get "//webadmin/port")
	fi

    # Add to vhost config
    echo "<VirtualHost *:${port}>" >${SITECONFFILE}
	echo "	ServerName $(omv_config_get "//services/website/hostname")">>${SITECONFFILE}

fi

# Get the shared folder reference and path
sfref=$(omv_config_get "//services/website/sharedfolderref")
sfpath=$(omv_get_sharedfolder_path "${sfref}")

cat <<EOF >>"/etc/apache2/sites-available/${SITECONF}"
	UseCanonicalName Off
	DirectoryIndex index.html index.htm index.php index.cgi index.pl
	ServerSignature Off
    DocumentRoot ${sfpath}
    ErrorLog \${APACHE_LOG_DIR}/omv-website-vhost_error.log
    CustomLog \${APACHE_LOG_DIR}/omv-website-vhost_access.log combined
EOF

# CGI user
CGIUSER=""
CGIGRP=""
USERUUID="$(omv_config_get "//services/website/cgiuser")"
if [ "${USERUUID}" != "" ]; then

	CGIUSER=$(omv_config_get "//system/usermanagement/users/user[uuid='${USERUUID}']/name")
	CGIGRP=$(groups "${CGIUSER}" | awk '{ print $3 }')

	if [ "${CGIUSER}" != "" ]; then
		chown -R ${CGIUSER}:${CGIGRP} /var/www/omv-website-vhost
	fi

fi


# Compose options
xmlstarlet sel -t -m "//services/website" \
	-i "ExecCGI[. = '1']" \
		-o "SuexecUserGroup ${CGIUSER} ${CGIGRP}" -n \
    	-o "	&lt;FilesMatch \.php$&gt;" -n \
        -o "		FcgidWrapper /var/www/omv-website-vhost/php-fcgi .php" -n \
		-o "		Options ExecCGI" -n \
        -o "		SetHandler fcgid-script" -n \
    	-o "	&lt;/FilesMatch&gt;" -n -b \
	-o "	&lt;Directory ${sfpath}&gt;" -n \
	-o "		Options " \
	-o "-ExecCGI " \
    -i "Indexes[. = '0']" -o "-" -b \
    -i "Indexes[. = '1']" -o "+" -b \
    -o "Indexes " \
    -i "FollowSymLinks[. = '0']" -o "-" -b \
    -i "FollowSymLinks[. = '1']" -o "+" -b \
    -o "FollowSymLinks " \
    -i "SymLinksIfOwnerMatch[. = '0']" -o "-" -b \
    -i "SymLinksIfOwnerMatch[. = '1']" -o "+" -b \
    -o "SymLinksIfOwnerMatch " \
    -i "Includes[. = '0']" -o "-" -b \
    -i "Includes[. = '1']" -o "+" -b \
    -o "Includes " \
    -i "MultiViews[. = '0']" -o "-" -b \
    -i "MultiViews[. = '1']" -o "+" -b \
    -o "MultiViews" -n \
	-o "AllowOverride " \
	-i "AuthConfig[. = '1']" -o "AuthConfig " -b \
	-i "OverrideIndexes[. = '1']" -o "Indexes " -b \
	-i "Limit[. = '1']" -o "Limit " -b \
	-i "Options[. = '1']" -o "Options " -b \
	-i "AuthConfig[. = '0'] and OverrideIndexes[. = '0'] and Limit[. = '0'] and Options[. = '0']" \
		-o "None" -b \
	-n -v "extraoptions" -n \
	-o "	&lt;/Directory&gt;" -n \
  	-i "enablessl[. = '1']" \
      -o "    SSLEngine on" -n \
      -v "concat('    SSLCertificateFile ${OMV_CERTIFICATE_DIR}/certs/',sslcertificateref,'.crt')" -n \
      -v "concat('    SSLCertificateKeyFile ${OMV_CERTIFICATE_DIR}/private/',sslcertificateref,'.key')" -n \
	-b \
	-o "&lt;/VirtualHost&gt;" -n \
	${OMV_CONFIG_FILE} | xmlstarlet unesc >>${SITECONFFILE}

# Run a configtest?
if [ "$2" = "configtest" ]; then
	a2ensite "$SITECONF" 2>&1 >/dev/null
	apache2ctl configtest

else 
	a2ensite "$SITECONF"
fi


